package com.jingyanzi.finance.service.impl;

import java.util.ArrayList;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.finance.dao.CashCashierMapper;

import com.jingyanzi.finance.domain.CashCashierDO;
import com.jingyanzi.finance.domain.CashPlaceDO;

import com.jingyanzi.finance.entity.CashCashier;
import com.jingyanzi.finance.entity.CashCashierExample;
import com.jingyanzi.finance.entity.CashCashierExample.Criteria;
import com.jingyanzi.finance.query.CashCashierQueryDO;
import com.jingyanzi.finance.result.ResultDO;
import com.jingyanzi.finance.result.ResultSupport;
import com.jingyanzi.finance.service.ICashierService;
import com.jingyanzi.finance.service.IPlaceService;
import com.jingyanzi.finance.util.BeanUtilsExtends;
import com.jingyanzi.privilege.domain.EmployeeDO;
import com.jingyanzi.privilege.factory.PrivilegeFactory;
import com.jingyanzi.privilege.service.IEmployeeService;

@Service("cashierService")
public class CashierServiceImpl extends BaseServiceImpl implements ICashierService {
	
	@Autowired
	private CashCashierMapper cashCashierMapper;
	
	@Autowired
	private IPlaceService placeService;
	
	private static Logger logger = Logger.getLogger(BillServiceImpl.class);
	
	private IEmployeeService getEmployeeService() {
		return PrivilegeFactory.getEmployeeService();
	}
	

	@Override
	public ResultDO add(Integer[] employeeIds) {
		
		CashCashier entity = new CashCashier();
	
		ResultSupport result = new ResultSupport();
	
		for(Integer id : employeeIds) {
			EmployeeDO employeeDO = getEmployee(id);
			
			System.out.println(employeeDO.getDepartment());
			entity.setName(employeeDO.getRealName());
			entity.setDepartment(employeeDO.getDepartment());
			entity.setTelephone(employeeDO.getMobile());
			entity.setEmployeeId(id);
			
	        try {
	            cashCashierMapper.insert(entity);
	        } catch (Exception e) {
	        	result.setErrorCode(ResultSupport.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "添加收银员" + employeeDO.getRealName() + "失败 ID:" + id);
	        	logger.error(e.getMessage());
	        }
	        
		}
     
        return result;
	}
	
	public ResultDO bindPlace(Integer[] cashierIds, Integer placeId) {
		
		CashCashier entity = new CashCashier();
		
		
		ResultSupport result = new ResultSupport();
		
		//通过收银员ID查询绑定的收银地点
		ResultDO res1 = placeService.get(placeId);
		if(!res1.isSuccess()) {
			result.setSuccess(false);
			result.setErrorCode(ResultSupport.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "pay 获取收银地点失败");
			logger.error(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "pay 获取收银地点失败");
			return result;
		}
		
		CashPlaceDO placeDO = (CashPlaceDO) res1.getModel(ResultSupport.FIRST_MODEL_KEY);
		
		for(Integer id : cashierIds) {
			System.out.println("====" + id);
			entity.setId(id);
			entity.setPlaceId(placeId);
			entity.setPlaceName(placeDO.getPlaceName());
			int r = -1;
	        
	        try {
	        	r =cashCashierMapper.updateByPrimaryKeySelective(entity);
	        } catch (Exception e) {
	        	e.printStackTrace();
	        	result.setSuccess(false);
				result.setErrorCode(ResultSupport.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "收银员绑定失败");
				logger.error(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "收银员绑定失败");
				return result;
	        }
	        
	        if (r < 0) {
	        	result.setSuccess(false);
				result.setErrorCode(ResultSupport.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "收银员绑定失败");
				logger.error(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "收银员绑定失败");
	        	return result;
	        }
		}
     
        return result;
	}
	
	public EmployeeDO getEmployee(int id) {
		//获取销售员工信息
		com.jingyanzi.privilege.
			domain.result.ResultDO res = getEmployeeService().getEmployee(id);
		
		if(!res.isSuccess()) {
			logger.error(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "没有销售人员");
			return null;
		}
		EmployeeDO employeeDO = (EmployeeDO) res.getModel(ResultSupport.FIRST_MODEL_KEY);
		return employeeDO;
	}
	
	@Override
	public ResultDO remove(Integer[] ids) {
		
		ResultSupport result = new ResultSupport();
		for(Integer id : ids) {
			ResultDO res = remove(id);
			if(!res.isSuccess()) {
				return res;
			}
		}
		
        return result;
	}


	
	public ResultDO remove(Integer id) {
	
		ResultSupport result = new ResultSupport();
		
		int r = -1;
        
        try {
            r = cashCashierMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
        	result.setSuccess(false);
			result.setErrorCode(ResultSupport.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "收银员解除绑定失败");
			logger.error(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "收银员解除绑定失败");
			return result;
        }
        
        if (r < 1) {
        	result.setSuccess(false);
			result.setErrorCode(ResultSupport.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "收银员解除绑定失败");
			logger.error(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "收银员解除绑定失败");
			return result;
        }
 
        return result;
	}

	@Override
	public ResultDO get(Integer id) {
		
		CashCashier entity = null;
		
		ResultSupport result = new ResultSupport();
		
	    try {
	    	entity = cashCashierMapper.selectByPrimaryKey(id);
	    } catch (Exception e) {
	    	result.setSuccess(false);
			result.setErrorCode(ResultSupport.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "获取收银员信息");
			logger.error(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "获取收银员信息");
			return result;
	    }
	    
	    CashCashierDO cashCashierDO = getCashCashierDO(entity);
	    
	    if(cashCashierDO == null) {
	    	result.setSuccess(false);
			result.setErrorCode(ResultSupport.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "获取收银员信息");
			logger.error(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + "获取收银员信息");
			return result;
	    }
	    result.setModel(ResultDO.FIRST_MODEL_KEY, cashCashierDO);
        return result;
	}
	
	@Override
	public ResultDO isEmployeeCashier(int employeeId) {
		
		ResultSupport result = new ResultSupport();
		
		CashCashierExample example = new CashCashierExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andEmployeeIdEqualTo(employeeId);
		
		List<CashCashier> cashCashiers = null;
		
		try {
			cashCashiers = cashCashierMapper.selectByExample(example);
		} catch (Exception e) {
			logger.error(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + e.getMessage());
		}
		CashCashierDO cashCashierDO = null;
		if(cashCashiers.size() != 0) {
			cashCashierDO = getCashCashierDO(cashCashiers.get(0));
		}
		result.setModel(ResultSupport.FIRST_MODEL_KEY, cashCashierDO);

		return result;
	}

	@Override
	public ResultDO query(CashCashierQueryDO query) {
		
		ResultSupport result = new ResultSupport();

		List<CashCashier> list = null;

		CashCashierExample example = new CashCashierExample();

		Criteria createCriteria = example.createCriteria();

		Criteria createCriteria1 = example.or();
		
		if(query.getEmployeeId() != null) {
			createCriteria1.andEmployeeIdEqualTo(query.getEmployeeId());
			createCriteria.andEmployeeIdEqualTo(query.getEmployeeId());
		}
		if(query.getNameOrTel()!=null&&!query.getNameOrTel().equals("")) {
			
			System.out.println(query.getNameOrTel());
			createCriteria1.andNameLike("%"+query.getNameOrTel()+"%");
			createCriteria.andTelephoneLike("%"+query.getNameOrTel()+"%");
		}
		
		if (query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = cashCashierMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}
		
		List<CashCashierDO> doList = null;

		try {
			list = cashCashierMapper.selectByExample(example);
		} catch (Exception e) {
			logger.error(ResultSupport.SYSTEM_EXCEPTION_ERROR_MSG + e.getMessage());
		}

		doList = getCashCashierDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, doList);

		return result;
	}
	
	
	public CashCashierDO getCashCashierDO(CashCashier entity) {
		if (entity == null)
			return null;
		CashCashierDO dst = new CashCashierDO();
		return BeanUtilsExtends.copyProperties(dst, entity) ? dst : null;
	}

	public List<CashCashierDO> getCashCashierDOList(List<CashCashier> list) {
		List<CashCashierDO> result = new ArrayList<CashCashierDO>();
		if (list != null && !list.isEmpty()) {
			for (CashCashier org : list) {
				CashCashierDO org1 = getCashCashierDO(org);
				if (org1 != null) {
					result.add(org1);
				}
			}
		}
		return result;
	}

}
